//
//  PhoneLogin.m
//  YIChuSeller
//
//  Created by admin on 2018/11/28.
//  Copyright © 2018 xiangshengjun. All rights reserved.
//

#import "PasswordLogin.h"
#import "GXRegisterViewController.h"
#import "GXMainTabBarController.h"
#import "LoginAndResetManager.h"
@interface PasswordLogin ()
@property (weak, nonatomic) IBOutlet UITextField *nameTextField;
@property (weak, nonatomic) IBOutlet UITextField *passwordTF;

@end
@implementation PasswordLogin
- (void)awakeFromNib{
    [super awakeFromNib];
}
+(instancetype)passwordLogin
{
    return [[NSBundle mainBundle]loadNibNamed:@"PasswordLogin" owner:nil options:nil].lastObject;
}
- (IBAction)clickPasswordOpenBtn:(UIButton *)sender {
    sender.selected = !sender.selected;
    if (sender.selected) {
        NSString *tempPwdStr = self.passwordTF.text;
        self.passwordTF.text = @""; //防止切换的时候光标偏移
        self.passwordTF.secureTextEntry = NO;
        self.passwordTF.text = tempPwdStr;
    } else { // 暗文
        NSString *tempPwdStr = self.passwordTF.text;
        self.passwordTF.text = @"";
        self.passwordTF.secureTextEntry = YES;
        self.passwordTF.text = tempPwdStr;
    }
}
- (IBAction)pressRegistBtn:(UIButton *)sender {
    GXRegisterViewController * registVC = [[GXRegisterViewController alloc]init];
    [self.getViewController presentViewController:registVC animated:YES completion:nil];
}
- (IBAction)pressLoginBtn:(UIButton *)sender {
    if (![self checkTextFieldValue]) {
        return;
    }
    [self.nameTextField resignFirstResponder];
    [self.passwordTF resignFirstResponder];
     NSString * passwordStr = [NSString MD5ForUpper32Bate:self.passwordTF.text];
    [LoginAndResetManager loginWithPhone:self.nameTextField.text password:passwordStr success:^(id responseObject) {
        NSLog(@"---%@",responseObject);
        [self handleSucessLoginData:responseObject];
    } fail:^(id responseObject) {
    }];

//    [HttpHelper Post:@"ycs/User/User.do" parameters:jsonStr success:^(id responseObject) {
//        if ([responseObject[@"cod"] isEqualToString:@"001"]) {
//            [SVProgressHUD showSuccessWithStatus:@"登录成功"];
//            AccountModel *userModel = [AccountModel mj_objectWithKeyValues: responseObject[@"result"]];
//            [[AccountTool sharedAccountTool] saveAccount:userModel];
//            NSString * phoneNumber = userModel.users[@"phone"];
//            [[NSUserDefaults standardUserDefaults] setObject:phoneNumber forKey:@"phoneNumber"];
//             [[NSUserDefaults standardUserDefaults]synchronize];
////            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
//                GXMainTabBarController * mainVC = [[GXMainTabBarController alloc]init];
//                UIWindow * mainWindow = [UIApplication sharedApplication].keyWindow;
//                mainVC.selectedIndex = 1;
//                mainWindow.rootViewController = mainVC;
////            });
//        }else{
//            [SVProgressHUD showErrorWithStatus:responseObject[@"describa"]];
//
//        }
//    } failure:^(NSError *error) {
//    }];
}
- (void)handleSucessLoginData:(id)responseObject
{
    AccountModel *userModel = [AccountModel mj_objectWithKeyValues: responseObject[@"result"]];
    [[AccountTool sharedAccountTool] saveAccount:userModel];
    NSString * phoneNumber = userModel.users[@"phone"];
    [[NSUserDefaults standardUserDefaults] setObject:phoneNumber forKey:@"phoneNumber"];
    [[NSUserDefaults standardUserDefaults]synchronize];
    GXMainTabBarController * mainVC = [[GXMainTabBarController alloc]init];
    UIWindow * mainWindow = [UIApplication sharedApplication].keyWindow;
    mainVC.selectedIndex = 1;
    mainWindow.rootViewController = mainVC;
}


- (BOOL)checkTextFieldValue
{
    if ([NSString isBlankString:self.nameTextField.text]){
        [SVProgressHUD showErrorWithStatus:@"手机号不能为空"];
        return NO;
    }
    if (self.nameTextField.text.length !=11) {
        [SVProgressHUD showErrorWithStatus:@"输入手机号不正确"];
        return NO;
    }
    if ([NSString isBlankString:self.passwordTF.text]) {
        [SVProgressHUD showErrorWithStatus:@"密码不能为空"];
        return NO;
    }
    return YES;
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self.nameTextField resignFirstResponder];
    [self.passwordTF resignFirstResponder];
}
@end
